package com.xnck.maventest.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xnck.maventest.entity.User;
import com.xnck.maventest.service.RootService;

@Controller
@RequestMapping(value="/")
public class RootController {
	
	@Autowired
	private RootService rootService;
	
	@RequestMapping(value="/index", method=RequestMethod.GET)
	public ModelAndView getIndex(@RequestParam(value="uid")String userId){
		User user = rootService.getUserById(userId);
		ModelAndView mv = new ModelAndView("index");
		mv.addObject("userId", user.getUserId());
		mv.addObject("userName", user.getUserName());
		return mv;
	}
	
	@RequestMapping(value="/test", method=RequestMethod.GET)
	public @ResponseBody Map<String, Object> getTest(@RequestParam(value="uid")String userId){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			rootService.createUser(userId);
			User user = rootService.getUserById(userId);
			map.put("uname", user.getUserName());
		} catch (Exception e) {
			map.put("error", e.toString());
		}
		return map;
	}
	
}
